package com.md.api.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class JsonUtil {

    public static final SerializerFeature[] DEFAULT_SERIALIZE_FEATURES = {
            SerializerFeature.QuoteFieldNames,
            SerializerFeature.SkipTransientField,
            SerializerFeature.SortField,
            SerializerFeature.WriteEnumUsingToString,
            SerializerFeature.WriteMapNullValue
    };

    public static String toJSONString(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue, SerializerFeature.QuoteFieldNames);
    }

    public static String toPrettyJSONString(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.QuoteFieldNames);
    }

    public static Map<String, Object> parse(String jsonText) {
        Map<String, Object> object = (Map<String, Object>) JSON.parse(jsonText);
        return object;
    }

    public static Map<String, Object> parse(Object obj) {
        Map<String, Object> object = (Map<String, Object>) JSON.parse(JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue, SerializerFeature.QuoteFieldNames));
        return object;
    }

    public static <T> T parse(String jsonText, Class<T> clazz) {
        T object = JSON.parseObject(jsonText, clazz);
        return object;
    }

    @SuppressWarnings("rawtypes")
    public static List<Map<String, Object>> parseArray(String jsonText) {
        List<Map<String, Object>> result = (List) JSON.parseArray(jsonText);
        return result;
    }

    public static <T> List<T> parseArray(String jsonText, Class<T> clazz) {
        List<T> result = JSON.parseArray(jsonText, clazz);
        return result;
    }




}
